package cs437;
import com.trolltech.qt.core.*;
import com.trolltech.qt.gui.*;

public class TalentHandler extends QMainWindow
{
	
	//The Talent Widget
	TalentWidget tw;
	
	/* 
	 * Static Class xml files
	 */	
	public static final String Druid = "data/Druid.xml";
	public static final String Hunter = "data/Hunter.xml";
	public static final String Mage = "data/Mage.xml";
	public static final String Paladin = "data/Paladin.xml";
	public static final String Priest = "data/Priest.xml";
	public static final String Rogue = "data/Rogue.xml";
	public static final String Shaman = "data/Shaman.xml";
	public static final String Warlock = "data/Warlock.xml";
	public static final String Warrior = "data/Warrior.xml";
	public static final String[] classFileList = {Druid, Hunter, Mage, Paladin, Priest, Rogue, Shaman, Warlock, Warrior};
	public String currentFile;
	
    //Menu Items & Actions
	private QMenu fileMenu;
	private QMenu helpMenu;	
	private QAction openAct;
	private QAction exitAct;
	private QAction aboutAct;
	private QAction aboutQtJambiAct;
	private QHBoxLayout treeLayout;
	private String currentPath;
	

    /* 
     * Class/Point Setup Group box
     */    
    public QGroupBox levelSelectGroupBox;
    public QSlider levelSelectSlider;
    public QLabel levelSelectLabel;
    public QSpinBox levelSelectBox;
    public QComboBox classComboBox;
    public QLabel classSelectLabel;
    public QPushButton loadButton;
    
    /* 
     * Class/Point Setup Group box
     */    
    public QGroupBox pointHandlerGroupBox;
    public QLabel pointsLeftLabel;
    public QLabel pointsSpentLabel;
    public QLabel pointsSpentTree1;
    public QLabel pointsSpentTree2;
    public QLabel pointsSpentTree3;
    public QLCDNumber pointsLeftNumber;
    public QLCDNumber pointsSpentNumber;   
    public QLabel currentClass;
    
    /*
     * Tree Names and Points spent in them
     */
    
    public QLabel tree1Name;
    public QLabel tree2Name;
    public QLabel tree3Name;
	

    
    //Main
    public static void main(String[] args) {
        QApplication.initialize(args);

        TalentHandler testTalentHandler = new TalentHandler(null);
        testTalentHandler.show();
        
        
        QApplication.exec();
        
    }
    
    public TalentHandler(QWidget parent){
        super(parent);
        createActions();
        createMenus();
        //inititalScreen();
        setUpUI();

        
        this.setWindowTitle("World of Warcraft Talent Builder");
        this.resize(1024, 720);
        this.setStyleSheet("background-color: transparent;");
        currentPath = QDir.homePath();
    }
    
    
    
    private void createActions()
    {
        exitAct = new QAction(tr("E&xit"), this);
        exitAct.setShortcut(tr("Ctrl+Q"));
        exitAct.setStatusTip(tr("Exit the application"));
        exitAct.triggered.connect(this, "close()");
        
        aboutAct = new QAction(tr("&About"), this);
        aboutAct.setStatusTip(tr("Show the application's About box"));
        aboutAct.triggered.connect(this, "about()");
        
        aboutQtJambiAct = new QAction(tr("About &Qt Jambi"), this);
        aboutQtJambiAct.setStatusTip(tr("Show the Qt Jambi's About box"));
        aboutQtJambiAct.triggered.connect(QApplication.instance(), "aboutQtJambi()");
        
        openAct = new QAction(tr("&Open"), this);
        openAct.setShortcut(tr("Ctrl+O"));
        openAct.triggered.connect(this, "open()");
       
   
    }
    
    private void createMenus()
    {
        fileMenu = menuBar().addMenu(tr("&File"));
        fileMenu.addAction(openAct);
        fileMenu.addAction(exitAct);

        helpMenu = menuBar().addMenu(tr("&Help"));
        helpMenu.addAction(aboutAct);
        helpMenu.addAction(aboutQtJambiAct);
    }
    private void inititalScreen()
    {
    	QGraphicsScene selectScene = new QGraphicsScene();
    	ClassSelectItem classObj = new ClassSelectItem("img/druid/druid.png");
    	classObj.setPos(new QPointF(0, 100));
    	selectScene.addItem(classObj);
    	
    	classObj = new ClassSelectItem("img/hunter/hunter.png");
    	classObj.setPos(new QPointF(190, 100));
    	selectScene.addItem(classObj);
    	
    	classObj = new ClassSelectItem("img/mage/mage.png");
    	classObj.setPos(new QPointF(370, 100));
    	selectScene.addItem(classObj);
 
    	classObj = new ClassSelectItem("img/paladin/paladin.png");
    	classObj.setPos(new QPointF(550, 100));
    	selectScene.addItem(classObj);
    	
    	classObj = new ClassSelectItem("img/priest/priest.png");
    	classObj.setPos(new QPointF(0, 275));
    	selectScene.addItem(classObj);
    	
    	classObj = new ClassSelectItem("img/rogue/rogue.png");
    	classObj.setPos(new QPointF(190, 275));
    	selectScene.addItem(classObj);
    
    	classObj = new ClassSelectItem("img/shaman/shaman.png");
    	classObj.setPos(new QPointF(370, 275));
    	selectScene.addItem(classObj);
    	
    	classObj = new ClassSelectItem("img/warlock/warlock.png");
    	classObj.setPos(new QPointF(550, 275));
    	selectScene.addItem(classObj);
    	
    	classObj = new ClassSelectItem("img/warrior/warrior.png");
    	classObj.setPos(new QPointF(0, 450));
    	selectScene.addItem(classObj);
    	
    	QGraphicsView selectView = new QGraphicsView(this);
    	selectView.setScene(selectScene);
    	selectView.setGeometry(75, 50, 800, 540);
    	
    }
    
    
    private void setUpUI()
    {         	
    	QGraphicsScene initialGS = new QGraphicsScene();
    	QFrame f = new QFrame(this);
    	f.setStyleSheet("background-image: url(\"img/structure/containerBG.png\");");
    	f.setGeometry(0, 20, 800, 700);
    	f.show();
    	
    	levelSelectGroupBox = new QGroupBox(this);
    	levelSelectGroupBox.setObjectName("levelSelectGroupBoxx");
    	levelSelectGroupBox.setGeometry(new QRect(800, 25, 220, 160));
        levelSelectGroupBox.setBackgroundRole(QPalette.ColorRole.Light);
        levelSelectGroupBox.setToolTip("Select Level to start from");
        levelSelectGroupBox.setStyleSheet("QGroupBox { background-image: url(\"img/structure/containerPattern.png\"); background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #E0E0E0, stop: 1 #FFFFFF); border: 2px solid gray;" +
		"border-radius: 5px; margin-top: 1ex; }");
        
        levelSelectLabel = new QLabel(levelSelectGroupBox);
        levelSelectLabel.setObjectName("levelSelectLabel");
        levelSelectLabel.setGeometry(10, 65, 150, 16);
        levelSelectLabel.setText("<h3><font color=\"gray\">Select Level: (10-70)</font><h3>");
        levelSelectSlider = new QSlider(levelSelectGroupBox);
        levelSelectSlider.setObjectName("levelSelectSlider");
        levelSelectSlider.setGeometry(new QRect(60, 85, 131, 31));
        levelSelectSlider.setFocusPolicy(com.trolltech.qt.core.Qt.FocusPolicy.StrongFocus);
        levelSelectSlider.setMinimum(10);
        levelSelectSlider.setMaximum(70);
        levelSelectSlider.setSliderPosition(70);
        levelSelectSlider.setOrientation(com.trolltech.qt.core.Qt.Orientation.Horizontal);
        levelSelectSlider.setTickPosition(com.trolltech.qt.gui.QSlider.TickPosition.TicksBothSides);
        levelSelectSlider.setTickInterval(5);     
        
        levelSelectBox = new QSpinBox(levelSelectGroupBox);
        levelSelectBox.setObjectName("levelSelectBox");
        levelSelectBox.setGeometry(new QRect(10, 89, 42, 22));
        levelSelectBox.setFocusPolicy(com.trolltech.qt.core.Qt.FocusPolicy.WheelFocus);
        levelSelectBox.setMinimum(10);
        levelSelectBox.setMaximum(70);
        levelSelectBox.setValue(70);
        
        classSelectLabel = new QLabel(levelSelectGroupBox);
        classSelectLabel.setObjectName("classSelectLabel");
        classSelectLabel.setGeometry(new QRect(10, 10, 150, 20));
        classSelectLabel.setText("<h3><font color=\"gray\">Select Class</font><h3>");
        
        classComboBox = new QComboBox(levelSelectGroupBox);
        classComboBox.setObjectName("classComboBox");
        classComboBox.setGeometry(new QRect(10, 30, 181, 22));
        classComboBox.setFocusPolicy(com.trolltech.qt.core.Qt.FocusPolicy.WheelFocus);
      
        classComboBox.clear();
        classComboBox.addItem(new QIcon(new QPixmap("img/druid/druid.png")), "Druid");        
        classComboBox.addItem(new QIcon(new QPixmap("img/hunter/hunter.png")), "Hunter");
        classComboBox.addItem(new QIcon(new QPixmap("img/mage/mage.png")), "Mage");
        classComboBox.addItem(new QIcon(new QPixmap("img/paladin/paladin.png")), "Paladin");
        classComboBox.addItem(new QIcon(new QPixmap("img/priest/priest.png")), "Priest");
        classComboBox.addItem(new QIcon(new QPixmap("img/druid/Rogue.png")), "Rogue");
        classComboBox.addItem(new QIcon(new QPixmap("img/shaman/shaman.png")), "Shaman");
        classComboBox.addItem(new QIcon(new QPixmap("img/warlock/warlock.png")), "Warlock");
        classComboBox.addItem(new QIcon(new QPixmap("img/warrior/warrior.png")), "Warrior");
        classComboBox.setStyleSheet("QComboBox {background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #E0E0E0, stop: 1 #FFFFFF); border: 2px solid gray;" +
		"border-radius: 5px; margin-top: 1ex; }");
             
        
        loadButton = new QPushButton(levelSelectGroupBox);
        loadButton.setObjectName("loadButton");
        loadButton.setText("Load");
        loadButton.setGeometry(new QRect(80, 130, 60, 25));
        loadButton.setFocusPolicy(com.trolltech.qt.core.Qt.FocusPolicy.StrongFocus);
        loadButton.clicked.connect(this, "loadTree()");
        
        
        //Signal Connection
        levelSelectSlider.valueChanged.connect(levelSelectBox, "setValue(int)");
        levelSelectBox.valueChanged.connect(levelSelectSlider, "setValue(int)");
       
        
        //Point Handler Group
        pointHandlerGroupBox= new QGroupBox(this);
        pointHandlerGroupBox.setGeometry(new QRect(800, 200, 220, 160));
        pointHandlerGroupBox.setStyleSheet("QGroupBox { background-image: url(\"img/structure/containerPattern.png\"); background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #E0E0E0, stop: 1 #FFFFFF); border: 2px solid gray;" +
		"border-radius: 5px; margin-top: 1ex; }");
        
        pointsLeftLabel = new QLabel(pointHandlerGroupBox);
        pointsLeftLabel.setText("Points Left:");
        pointsLeftLabel.setGeometry(new QRect(10, 10, 60, 16));
        
        pointsSpentLabel = new QLabel(pointHandlerGroupBox);
  
        pointsSpentLabel.setText("Points Spent:");
        pointsSpentLabel.setGeometry(new QRect(10, 30, 60, 16));
        
        pointsLeftNumber = new QLCDNumber(pointHandlerGroupBox);
        pointsLeftNumber.setObjectName("pointsLeftNumber");
        pointsLeftNumber.setGeometry(new QRect(80, 10, 32, 16));
        pointsLeftNumber.setForegroundRole(QPalette.ColorRole.Highlight);
        pointsLeftNumber.setSegmentStyle(QLCDNumber.SegmentStyle.Filled);
        pointsLeftNumber.display(levelSelectBox.value()-9);
        
        
        pointsSpentNumber = new QLCDNumber(pointHandlerGroupBox);
        pointsSpentNumber.setObjectName("pointsSpentNumber");
        pointsSpentNumber.setGeometry(new QRect(80, 30, 32, 16));
        
        levelSelectBox.valueChanged.connect(this, "updateLevel(int)");
        
        currentClass = new QLabel(this);
        currentClass.setObjectName("Logo");
        currentClass.setGeometry(new QRect(820, 350, 180, 170));
        
  
        
        tw = new TalentWidget(this);
    	tw.setGeometry(20, 40, 765, 640);	
    	tw.setTotalPoints(levelSelectBox.value());
    	tw.P.connect(this, "pointChange(int)");
    	tw.hovering.connect(this, "catchHover(Talent)");
        
       
        
    }
    protected void open()
    {
    	String fileName = QFileDialog.getOpenFileName(this);
        
        // if (!fileName.isEmpty())
           //openImage(fileName);
    }
    
    
    
    protected void about() {
    	QMessageBox.information(this, "Info", "It's your turn now :-)");
    }
    public void mousePressEvent(QMouseEvent event) {
		
	    if (event.button() == Qt.MouseButton.LeftButton) {
	    		      
	    	System.out.println("Handler:Left Mouse Clicked");
	    	
	    	event.accept();
	       
	    }
	    if (event.button() == Qt.MouseButton.RightButton) {
	    		      
	    	System.out.println("Handler:Right Mouse Clicked");
	    	
	        event.accept();
	                
	    }
	    this.update();	    
	}
    
    public void pointChange(int change)
    {
    	pointsSpentNumber.display(tw.getPointsSpent());   	
    	pointsLeftNumber.display(tw.getPointsLeft());
    }
    public void updateLevel(int x)
    {
    	pointsLeftNumber.display(x-9);
    }
    public void loadTree()
    {
    	pointsSpentNumber.display(0);
    	String s = "img/" + classComboBox.currentText() + "/" + classComboBox.currentText() + ".png";
    	currentFile = classFileList[classComboBox.currentIndex()];
    	pointsLeftNumber.display(levelSelectBox.value()-9);
    	tw.setTotalPoints(levelSelectBox.value()-9);
    	tw.load(currentFile);
    	currentClass.setPixmap(new QPixmap(s));
    }
    
    
    public void catchHover(Talent t)
    {
    	System.out.println(t.getName());
    	RankWindow rw = new RankWindow(t, this);
    	rw.setGeometry(810, 515, 200, 120);
    	QGraphicsScene selectScene = new QGraphicsScene();
    	//selectScene.addItem(rw);
    	QGraphicsView selectView = new QGraphicsView(this);
    	//selectView.show();
    	selectView.setScene(selectScene);
    	rw.repaint();
    	//selectView.setGeometry(810, 515, 200, 120);
    }
    
    
    
    
    
    
    
    
    
    
    
  //Rank Window
	private class ClassSelectItem extends QGraphicsItem
	{
		String gfx;
		public ClassSelectItem(String gfx)
		{
			this.gfx = gfx;
		}
		
		private QRectF boundingRect = new QRectF(0, 0, 180, 170);
		public QRectF boundingRect()
		{
			return boundingRect;
		}
		
		/***************************************************** 
		 * Shape: This function is an interface function QT requires of QGraphicItems
		 * Shape defines the shape of the object; Used in collision detection
		 * Ours has a defined height and width
		 *****************************************************/
		QPainterPath shape = new QPainterPath();
		{
			shape.addRoundRect(boundingRect, 20);
		}
		
		public QPainterPath shape() {
			return shape;
		}
		
		/*****************************************************
		 * Paint: This function is an interface function QT requires of QGraphicItems
		 * Purpose: Draws the GraphicsItem on screen using a combination of Brushes, Pens and Pixmaps. 
		 *****************************************************/
		public void paint(QPainter painter, QStyleOptionGraphicsItem option,
		QWidget widget)
		{
	
			//Brush & Light Blue color for BG		
			QBrush brush = new QBrush(QColor.black);			
			if (!scene().collidingItems(this).isEmpty())
			{
				//brush.setColor(QColor.red);
			}			
			/****************************************************
			 * Drawing the Child Connection
			 * If the talent has a child, it draws an arrow from itself to the child
			 * This is done by subtracting the talent's tier from the child's tier # then multiplying that by the height.
			 * The child is always in the same x-position
			 * S
			 *****************************************************/
			
			
			painter.drawPath(shape);		

			// Draw the Icon associated with this talent	
			painter.setPen(QColor.darkGray);
			//Draw the Box that borders the ranks/ranksAchieved
			QRectF box = boundingRect;		
			QPainterPath pp = new QPainterPath();		
			pp.addRoundRect(box, 25);
			
			painter.drawPath(pp);			
			
	    	QPixmap image = new QPixmap(gfx);
	    	painter.drawPixmap(new QPoint(0, 0), image);
	    	
			
			
		}
	}
}
